// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   IPModemDriver.java

package cn.sendsms;

import java.io.*;
import java.util.StringTokenizer;
import org.apache.commons.net.telnet.*;
import org.apache.log4j.Logger;

// Referenced classes of package cn.sendsms:
//			ModemDriver, GatewayException, ModemGateway

class IPModemDriver extends ModemDriver
{
	private class Peeker extends Thread
	{

		public void run()
		{
			do
				try
				{
					do
					{
						if (tc != null && portHasData())
							synchronized (SYNC_Reader)
							{
								dataReceived = true;
								SYNC_Reader.notifyAll();
							}
						sleep(500L);
					} while (true);
				}
				catch (InterruptedException e)
				{
					if (tc == null)
						break;
				}
				catch (Exception e) { }
			while (true);
			gateway.getLogger().debug("Peeker thread ended.");
		}

		public Peeker()
		{
			super();
			setPriority(1);
			start();
			gateway.getLogger().debug("Peeker thread started.");
		}
	}


	private String ipAddress;
	private int ipPort;
	private TelnetClient tc;
	private InputStream in;
	private OutputStream out;
	private Peeker peeker;
	TerminalTypeOptionHandler ttopt;
	EchoOptionHandler echoopt;
	SuppressGAOptionHandler gaopt;

	IPModemDriver(ModemGateway gateway, String deviceParms)
	{
		super(gateway, deviceParms);
		ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, false);
		echoopt = new EchoOptionHandler(true, false, true, false);
		gaopt = new SuppressGAOptionHandler(true, true, true, true);
		StringTokenizer tokens = new StringTokenizer(deviceParms, ":");
		ipAddress = tokens.nextToken();
		ipPort = Integer.parseInt(tokens.nextToken());
		tc = null;
	}

	void connectPort()
		throws GatewayException, IOException, InterruptedException
	{
		try
		{
			gateway.getLogger().info("Opening: " + ipAddress + " @" + ipPort);
			tc = new TelnetClient();
			tc.addOptionHandler(ttopt);
			tc.addOptionHandler(echoopt);
			tc.addOptionHandler(gaopt);
			tc.connect(ipAddress, ipPort);
			in = tc.getInputStream();
			out = tc.getOutputStream();
			peeker = new Peeker();
			setConnected(true);
		}
		catch (InvalidTelnetOptionException e)
		{
			throw new GatewayException("Unsupported telnet option for the selected IP connection.");
		}
	}

	void disconnectPort()
		throws IOException, InterruptedException
	{
		gateway.getLogger().info("Closing: " + ipAddress + " @" + ipPort);
		synchronized (SYNC_Reader)
		{
			if (tc != null)
				tc.disconnect();
			tc = null;
			peeker.interrupt();
			peeker.join();
		}
		setConnected(false);
	}

	void clear()
		throws IOException
	{
		for (; portHasData(); read());
	}

	boolean portHasData()
		throws IOException
	{
		return in.available() > 0;
	}

	void write(char c)
		throws IOException
	{
		out.write((short)c);
		out.flush();
	}

	int read()
		throws IOException
	{
		return in.read();
	}

}
